<?php

namespace admin\controllers;

use app\models\Ad;
use admin\components\BaseAuthController;
use app\models\ar\UserAr;
use app\models\PayLog;
use app\models\UserProfile;

class AdController extends BaseAuthController {

    public $name = '广告管理';

    public function actionIndex() {
        $wd = $this->get('wd');
        $search_type = intval($this->get('search_type'));

        $model = new Ad();
        if ($search_type === 1 && !empty($wd)) {
            $profile = UserProfile::find()->where(['nickname' => $wd])->one();
            $model->where = ['uid' => $profile ? $profile->uid : 0];
        }
        if ($search_type === 2 && !empty($wd)) {
            $userAr = UserAr::find()->where(['phone' => $wd])->one();
            $model->where = ['uid' => $userAr ? $userAr->id : 0];
        }
        if ($search_type === 3 && !empty($wd)) {
            $model->where = ['like', 'title', $wd];
        }

        $model->with = ['profile'];
        $dataProvider = $model->search($this->getParam());
        return $this->render(compact('dataProvider'));
    }

    public function actionView($id) {
        return $this->render(['model' => $this->findModel($id, Ad::className())]);
    }

    public function actionAwards($id) {
        $model = new PayLog();
        $dataProvider = $model->search(['ad_id' => $id, 'type' => PayLog::TYPE_AD]);
        return $this->render(compact('dataProvider'));
    }
}
